package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.service.LogHelper;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.VendorTag;
import org.linlinjava.litemall.db.service.VendorTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/vendor/tag")
@Validated
public class AdminVendorTagController {
    private final Log logger = LogFactory.getLog(AdminVendorTagController.class);

    @Autowired
    private VendorTagService tagService;
    @Autowired
    private LogHelper logHelper;

    @RequiresPermissions("admin:vendortag:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "标签管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<VendorTag> tagList = tagService.queryTags(name, page, limit, sort, order);
        return ResponseUtil.okList(tagList);
    }
    @RequiresPermissions("admin:vendortag:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "标签管理"}, button = "查询所有")
    @GetMapping("/listAll")
    public Object listAll() {
        List<VendorTag> tagList = tagService.queryTags(null, 1, Integer.MAX_VALUE, null, null);
        return ResponseUtil.okList(tagList);
    }

    private Object validate(VendorTag tag) {
        if(StringUtils.isEmpty(tag.getName())) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    @RequiresPermissions("admin:vendortag:create")
    @RequiresPermissionsDesc(menu = {"商品管理", "标签管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody VendorTag tag) {
        Object error = validate(tag);
        if (error != null) {
            return error;
        }
        if(null == tag.getSortOrder()) {
            tag.setSortOrder(100);
        }
        tagService.addTag(tag);
        return ResponseUtil.ok(tag);
    }

    @RequiresPermissions("admin:vendortag:create")
    @RequiresPermissionsDesc(menu = {"商品管理", "标签管理"}, button = "批量添加")
    @PostMapping("/createBatch")
    public Object createBatch(@RequestBody List<VendorTag> tags) {
        for(VendorTag tag : tags) {
            Object error = validate(tag);
            if (error != null) {
                return error;
            }
            if(null == tag.getSortOrder()) {
                tag.setSortOrder(100);
            }
            tagService.addTag(tag);
        }
        return ResponseUtil.ok(tags);
    }

    @RequiresPermissions("admin:vendortag:read")
    @RequiresPermissionsDesc(menu = {"商品管理", "标签管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Long id) {
        VendorTag tag = tagService.findById(id);
        return ResponseUtil.ok(tag);
    }

    @RequiresPermissions("admin:vendortag:update")
    @RequiresPermissionsDesc(menu = {"商品管理", "标签管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody VendorTag tag) {
        Object error = validate(tag);
        if (error != null) {
            return error;
        }
        if (tagService.updateById(tag) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(tag);
    }

    @RequiresPermissions("admin:vendortag:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "标签管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody VendorTag tag) {
        Long id = tag.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        tagService.deleteById(id);
        return ResponseUtil.ok();
    }

}
